package com.sunmnet.j2ee.service.powerdb.designer.impl;

import com.sunmnet.j2ee.core.powerdb.designer.Commons;
import com.sunmnet.j2ee.core.pub.sqlloader.SqlLoader;
import com.sunmnet.j2ee.dao.BaseDao;
import com.sunmnet.j2ee.entity.powerdb.designer.PdmModule;
import com.sunmnet.j2ee.service.impl.BaseServiceImpl;
import com.sunmnet.j2ee.service.powerdb.designer.ModuleService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 蔡岳衡 on 2016/8/4.
 */

@Service("/powerdb/designer/moduleService")
public class ModuleServiceImpl extends BaseServiceImpl<PdmModule, Long> implements ModuleService {

	/**
	 * log4j2日志 *
	 */
	private static final Logger LOG = LogManager.getLogger(DbConnInfoServiceImpl.class.getName());

	private static final String CLASS_NAME = ModuleServiceImpl.class.getName();

	private static final String SQL_SELECT_MODULE =
			SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_MODULE");

	private static final String SQL_SELECT_ENTITY =
			SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_ENTITY");
	private static final String SQL_SELECT_MODULE_BY_PROJECT =
			SqlLoader.getInstance().getSql(CLASS_NAME, "SQL_SELECT_MODULE_BY_PROJECT");

	private static final String FIND_SQL_FOR_SAVE = "from PdmModule t where upper(t.name)=upper(:name) AND t.projectId = :projectId  AND t.versionId = " + Commons.DEF_VERSION_ID;
	private static final String FIND_SQL_FOR_UPDATE = "from PdmModule t where upper(t.name)=upper(:name) AND t.projectId = :projectId  and t.id!=:id AND t.versionId = " + Commons.DEF_VERSION_ID;

	@Override
	@Resource(name = "/powerdb/designer/moduleDao")
	public void setDao(BaseDao<PdmModule, Long> baseDao) {
		super.dao = baseDao;
	}

	@Override
	public int checkRepeat(PdmModule module) {

		Map params = new HashMap();
		params.put("name", module.getName());
		params.put("projectId", module.getProjectId());

		String sql = FIND_SQL_FOR_SAVE;
		if (module.getId() != null) {
			sql = FIND_SQL_FOR_UPDATE;
			params.put("id", module.getId());
		}

		List list = dao.find(sql, params);
		if (list != null && list.size() > 0) {
			LOG.debug("模块" + module.getName() + "已经存在");
			return 1;
		}
		return 0;
	}

	@Override
	public PdmModule get(Long id, Long versionId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("versionId", versionId);
		String hql = "FROM PdmModule e WHERE e.id = :id AND e.versionId = :versionId";
		return dao.getByHql(hql, params);
	}

	@Override
	public List getModuleById(long moduleId) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("moduleId", moduleId);
		return dao.findBySql(SQL_SELECT_MODULE, params);
	}

	/**
	 * 根据工程id获取相关模块
	 *
	 * @param projectId 工程id
	 * @return
	 * @throws Exception
	 */
	@Override
	public List getProjectRelModule(long projectId) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("projectId", projectId);
		return dao.findBySql(SQL_SELECT_MODULE_BY_PROJECT, params);
	}

	/**
	 * 根据模块id获取相关实体
	 *
	 * @param moduleId 模块id
	 * @return
	 * @throws Exception
	 */
	@Override
	public List getModuleRelEntity(long moduleId) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("moduleId", moduleId);
		return dao.findBySql(SQL_SELECT_ENTITY, params);
	}

	@Override
	public PdmModule copyModules(PdmModule module, long versionId) throws Exception {
		PdmModule tmp = clone(module, versionId);
		dao.save(tmp);

		LOG.debug("copyModules: {},{}, result:{} ", module, versionId, tmp);

		return tmp;
	}

	@Override
	public void deleteModuleRelations(long moduleId) throws Exception {

		String sql = "DELETE FROM pdm_entity WHERE module_id = " + moduleId;
		dao.executeSql(sql);

		sql = "DELETE FROM pdm_field WHERE module_id = " + moduleId;
		dao.executeSql(sql);

		sql = "DELETE FROM pdm_index_info WHERE module_id = " + moduleId;
		dao.executeSql(sql);

		sql = "DELETE FROM pdm_entity_relate WHERE module_id = " + moduleId;
		dao.executeSql(sql);
	}

	private PdmModule clone(PdmModule in, Long versionId) {
		PdmModule module = new PdmModule();

		module.setVersionId(versionId);
		module.setName(in.getName());
		module.setParentId(in.getParentId());
		module.setProjectId(in.getProjectId());
		module.setCreateTime(in.getCreateTime());
		module.setModifyTime(in.getModifyTime());
		module.setCreateAccountId(in.getCreateAccountId());
		module.setModifyAccountId(in.getModifyAccountId());
		module.setPathUrl(in.getPathUrl());
		module.setAccountId(in.getAccountId());
		module.setDatabaseId(in.getDatabaseId());
		module.setDbInfoId(in.getDbInfoId());
		module.setShorIndex(in.getShorIndex());
		module.setRemark(in.getRemark());

		return module;
	}
}
